package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.MedicinePriceDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDetailDAO;
import cn.edu.scau.cmi.hombio.dao.SupplierDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.MedicinePrice;
import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineStorageDetail;
import cn.edu.scau.cmi.hombio.domain.Supplier;

import cn.edu.scau.cmi.hombio.service.MedicineStorageService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for MedicineStorage entities
 * 
 */

@Controller("MedicineStorageRestController")

public class MedicineStorageRestController {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages MedicinePrice entities
	 * 
	 */
	@Autowired
	private MedicinePriceDAO medicinePriceDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorageDetail entities
	 * 
	 */
	@Autowired
	private MedicineStorageDetailDAO medicineStorageDetailDAO;

	/**
	 * DAO injected by Spring that manages Supplier entities
	 * 
	 */
	@Autowired
	private SupplierDAO supplierDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageService medicineStorageService;

	/**
	 * Create a new MedicinePrice entity
	 * 
	 */
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicinePrices", method = RequestMethod.POST)
	@ResponseBody
	public MedicinePrice newMedicineStorageMedicinePrices(@PathVariable Integer medicinestorage_id, @RequestBody MedicinePrice medicineprice) {
		medicineStorageService.saveMedicineStorageMedicinePrices(medicinestorage_id, medicineprice);
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice.getId());
	}

	/**
	* Show all MedicineStorage entities
	* 
	*/
	@RequestMapping(value = "/MedicineStorage", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicineStorage> listMedicineStorages() {
		return new java.util.ArrayList<MedicineStorage>(medicineStorageService.loadMedicineStorages());
	}

	/**
	* Get Supplier entity by MedicineStorage
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/supplier", method = RequestMethod.GET)
	@ResponseBody
	public Supplier getMedicineStorageSupplier(@PathVariable Integer medicinestorage_id) {
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id).getSupplier();
	}

	/**
	* Show all MedicinePrice entities by MedicineStorage
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicinePrices", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicinePrice> getMedicineStorageMedicinePrices(@PathVariable Integer medicinestorage_id) {
		return new java.util.ArrayList<MedicinePrice>(medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id).getMedicinePrices());
	}

	/**
	* Create a new MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage", method = RequestMethod.POST)
	@ResponseBody
	public MedicineStorage newMedicineStorage(@RequestBody MedicineStorage medicinestorage) {
		medicineStorageService.saveMedicineStorage(medicinestorage);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());
	}

	/**
	* Show all MedicineStorageDetail entities by MedicineStorage
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicineStorageDetails", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicineStorageDetail> getMedicineStorageMedicineStorageDetails(@PathVariable Integer medicinestorage_id) {
		return new java.util.ArrayList<MedicineStorageDetail>(medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id).getMedicineStorageDetails());
	}

	/**
	* Save an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicinePrices", method = RequestMethod.PUT)
	@ResponseBody
	public MedicinePrice saveMedicineStorageMedicinePrices(@PathVariable Integer medicinestorage_id, @RequestBody MedicinePrice medicineprices) {
		medicineStorageService.saveMedicineStorageMedicinePrices(medicinestorage_id, medicineprices);
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprices.getId());
	}

	/**
	* Delete an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorage(@PathVariable Integer medicinestorage_id) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id);
		medicineStorageService.deleteMedicineStorage(medicinestorage);
	}

	/**
	* Save an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee saveMedicineStorageEmployee(@PathVariable Integer medicinestorage_id, @RequestBody Employee employee) {
		medicineStorageService.saveMedicineStorageEmployee(medicinestorage_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Delete an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicinePrices/{medicineprice_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorageMedicinePrices(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_medicineprices_id) {
		medicineStorageService.deleteMedicineStorageMedicinePrices(medicinestorage_id, related_medicineprices_id);
	}

	/**
	* View an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/supplier/{supplier_id}", method = RequestMethod.GET)
	@ResponseBody
	public Supplier loadMedicineStorageSupplier(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_supplier_id) {
		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(related_supplier_id, -1, -1);

		return supplier;
	}

	/**
	* Delete an existing MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicineStorageDetails/{medicinestoragedetail_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorageMedicineStorageDetails(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_medicinestoragedetails_id) {
		medicineStorageService.deleteMedicineStorageMedicineStorageDetails(medicinestorage_id, related_medicinestoragedetails_id);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Save an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineStorage saveMedicineStorage(@RequestBody MedicineStorage medicinestorage) {
		medicineStorageService.saveMedicineStorage(medicinestorage);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());
	}

	/**
	* Save an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/supplier", method = RequestMethod.PUT)
	@ResponseBody
	public Supplier saveMedicineStorageSupplier(@PathVariable Integer medicinestorage_id, @RequestBody Supplier supplier) {
		medicineStorageService.saveMedicineStorageSupplier(medicinestorage_id, supplier);
		return supplierDAO.findSupplierByPrimaryKey(supplier.getId());
	}

	/**
	* Delete an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorageEmployee(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_employee_id) {
		medicineStorageService.deleteMedicineStorageEmployee(medicinestorage_id, related_employee_id);
	}

	/**
	* Delete an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/supplier/{supplier_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorageSupplier(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_supplier_id) {
		medicineStorageService.deleteMedicineStorageSupplier(medicinestorage_id, related_supplier_id);
	}

	/**
	* Save an existing MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicineStorageDetails", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineStorageDetail saveMedicineStorageMedicineStorageDetails(@PathVariable Integer medicinestorage_id, @RequestBody MedicineStorageDetail medicinestoragedetails) {
		medicineStorageService.saveMedicineStorageMedicineStorageDetails(medicinestorage_id, medicinestoragedetails);
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetails.getId());
	}

	/**
	* Select an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorage loadMedicineStorage(@PathVariable Integer medicinestorage_id) {
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id);
	}

	/**
	* View an existing MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicineStorageDetails/{medicinestoragedetail_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorageDetail loadMedicineStorageMedicineStorageDetails(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_medicinestoragedetails_id) {
		MedicineStorageDetail medicinestoragedetail = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(related_medicinestoragedetails_id, -1, -1);

		return medicinestoragedetail;
	}

	/**
	* Get Employee entity by MedicineStorage
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getMedicineStorageEmployee(@PathVariable Integer medicinestorage_id) {
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id).getEmployee();
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadMedicineStorageEmployee(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}

	/**
	* View an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicinePrices/{medicineprice_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicinePrice loadMedicineStorageMedicinePrices(@PathVariable Integer medicinestorage_id, @PathVariable Integer related_medicineprices_id) {
		MedicinePrice medicineprice = medicinePriceDAO.findMedicinePriceByPrimaryKey(related_medicineprices_id, -1, -1);

		return medicineprice;
	}

	/**
	* Create a new Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/supplier", method = RequestMethod.POST)
	@ResponseBody
	public Supplier newMedicineStorageSupplier(@PathVariable Integer medicinestorage_id, @RequestBody Supplier supplier) {
		medicineStorageService.saveMedicineStorageSupplier(medicinestorage_id, supplier);
		return supplierDAO.findSupplierByPrimaryKey(supplier.getId());
	}

	/**
	* Create a new Employee entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newMedicineStorageEmployee(@PathVariable Integer medicinestorage_id, @RequestBody Employee employee) {
		medicineStorageService.saveMedicineStorageEmployee(medicinestorage_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Create a new MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorage/{medicinestorage_id}/medicineStorageDetails", method = RequestMethod.POST)
	@ResponseBody
	public MedicineStorageDetail newMedicineStorageMedicineStorageDetails(@PathVariable Integer medicinestorage_id, @RequestBody MedicineStorageDetail medicinestoragedetail) {
		medicineStorageService.saveMedicineStorageMedicineStorageDetails(medicinestorage_id, medicinestoragedetail);
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail.getId());
	}
}